const app = getApp()
//   YYYY-mm-dd HH:MM
import dateFormat from '../../utils/dateFormat';

Page({
  data: {
    order_list: [],
    active: "0",
    user_address: {},
    user_address_str: null,
  },
  // 评价
  pj(e) {
    let order_id = e.currentTarget.dataset.item.order_id
    console.log(e.currentTarget.dataset.item);
    wx.navigateTo({
      url: '/pages/appraise/appraise?order_id='+order_id,
    })
  },
  // 获取地址
  get_address() {
    let that = this
    wx.chooseAddress({
      success: res => {
        console.log(res);
        let user_address = {
          name: res.userName,
          provinceName: res.provinceName,
          cityName: res.cityName,
          detailInfo: res.detailInfo,
          telNumber: res.telNumber
        }
        let user_address_str = `${res.provinceName}-${res.cityName}-${res.detailInfo}`
        that.setData({
          user_address,
          user_address_str
        })
      }
    })
  },



  pay(e) {
    console.log(e);
    let order_id = e.currentTarget.dataset.item.order_id
    let order_type = e.currentTarget.dataset.item.order_type
    this.wxpay(order_id, order_type)
  },



  // 取消订单
  concel_order(e) {
    let {
      order_id,
      id,
      goods_id
    } = e.currentTarget.dataset.item
    console.log(id);
    console.log(e);
    app.request("/user/order_cancel", {
      method: "POST",
      data: {
        order_id,
        type: 1,
        id,
        goods_id
      }
    }).then(res => {
      console.log(res);
      wx.showToast({
        title: '取消成功',
        icon: 'none'
      })
      this.onShow()
    })
  },


  refund(e) {
    let {
      order_id,
      store_id,
      order_type
    } = e.currentTarget.dataset.item



    app.request("/user/add_refund", {
      method: "POST",
      data: {
        order_id,
        store_id,
        code: order_type
      }
    }).then(res => {
      if (res)
        if (res.data.code == 200) {
          this.get_list(7, 0)
        }
    })

  },



  fetch(e) {
    this.get_list(0, 1)
    this.get_list(0, 2)

  },
  // 获取订单
  get_order(e) {
    console.log(e);
    let type = e.detail.name
    if (type == 0) {
      this.get_list(type, 1)
      this.get_list(type, 2)
    } else {
      this.get_list(type, 0)
    }
  },

  // 确认收货
  confirm_receipt(e) {
    let {
      order_id,
      goods_id
    } = e.currentTarget.dataset.item
    app.request("/user/Confirm_receipt", {
      method: "POST",
      data: {
        order_no: order_id,
        goods_id
      }
    }).then(res => {
      wx.showToast({
        title: '已确认收货',
        icon: 'none'
      })
      console.log(res);
    })
  },


  get_list(type, order_type) {
    let that = this
    let params = {
      type: type
    }

    if (type == 1) {
      params.state = 1
    } else if (type == 2) {
      params.state = 2
      params.type = 1
    }
    // order_type不等于0
    if (order_type) {
      params.order_type = order_type
    }

    console.log(params);

    app.request("/user/get_orderlist", {
      data: params
    }).then(res => {
      console.log(res, "订单列表");
      let data = res.data.data
      let order_list = []
      if (order_type == 2) {
        order_list = data.concat(that.data.order_list)
      } else {
        order_list = data
      }

      if (order_list) {

        order_list.forEach(item => {
          item.add_time = dateFormat(" YYYY-mm-dd HH:MM", item.add_time * 1000)
          // console.log(item.sy_time);
          if (item.img) {
            if (!Array.isArray(item.img)) {
              item.img = [item.img]
            }
          }
          if (item.goods_pic) {
            item.goods_pic = [item.goods_pic]
          }
        })

        that.setData({
          order_list
        })
        console.log(this.data.order_list, "data-order_list");

      } else {
        console.log("订单为空");
        that.setData({
          order_list: []
        })
      }

    })
  },




  wxpay(order_no, order_type) {
    let that = this
    let {
      telNumber,
      name
    } = this.data.user_address
    let user_address_str = this.data.user_address_str

    app.request("/user/pay/aliPayApp_num", {
      method: "POST",
      data: {
        order_type,
        order_no,
        phone: telNumber,
        name: name,
        address: user_address_str
      }
    }).then(res => {
      console.log(res, "发起支付");
      if (res.data.code == 500) {
        wx.showToast({
          title: '请选择收货地址后支付',
          icon: "none",
          success() {
            that.get_address()
          }
        })
      }
      let {
        timeStamp,
        nonceStr,
        signType,
        paySign
      } = res.data.data
      let packages = res.data.data.package
      let out_trade_no = res.data.order_id
      wx.requestPayment({
        timeStamp: timeStamp + '',
        nonceStr: nonceStr,
        package: packages,
        signType: signType,
        paySign: paySign,
        fail(err) {
          console.log(err)
          wx.navigateTo({
            url: '/pages/order_list/order_list',
          })
        },
        success(res) {
          console.log(res, "支付成功");
          //查询订单

          app.request("/user/pay/invest_search", {
            method: "POST",
            data: {
              out_trade_no
            }
          }).then(res => {
            console.log(res, "订单查询");
            wx.navigateTo({
              url: '/pages/order_list/order_list',
            })
          })
        }
      })
    })
  },




  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.fetch()
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
 
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})